const FoodDao = require('../dao/food.dao.js');
const urlUtil = require("../../utils/httpUtil.js");
const config = require('../config/server.config');

const notify_url = config.notifyCustomerUrl;

//当添加修改或者删除food时，通知C端
let notify = function (_id, callback) {
    urlUtil.getUrlContent(notify_url + _id, function (data) {console.log(data)});
    console.log('notify!!!');
}

exports.insertFood = function (merchantId, food, callback) {
    FoodDao.insertFood(food, function (data) {
        callback(data);
        if (data.status == true) {
            console.log(data.data);
            let id = data.data.merchant;
            notify(id);
        }
    });
}

exports.deleteFood = function (merchantId, id, callback) {
    FoodDao.deleteFood(id, function (data) {
        callback(data);
        if (data.status == true) {
            notify(merchantId);
        }
    });
}

exports.updateFood = function (merchantId, food, callback) {
    FoodDao.updateFood(food, function (data) {
        callback(data);
        if (data.status == true) {
            let id = food._id;
            notify(merchantId);
        }
    });
}
exports.findAll = function (callback) {
    FoodDao.findAll(function (data) {
        callback(data);
    });
}
exports.findById = function (id, callback) {
    FoodDao.findById(id, function (data) {
        callback(data);
    });
}
exports.findByIds = function (ids, callback) {
    FoodDao.findByIds(ids, function (data) {
        callback(data);
    });
}
exports.findByMId = function (m_id, callback) {
    FoodDao.findByMId(m_id, function (data) {
        callback(data);
    });
}

exports.findByMIdandFTId = function (m_id, foodType_id, callback) {
    FoodDao.findByMIdandFTId(m_id, foodType_id, function (data) {
        callback(data);
    });
}

exports.findByName = function (foodName, callback) {
    FoodDao.findByName(foodName, function (data) {
        callback(data);
    });
}

exports.findByType = function (foodType, callback) {
    FoodDao.findByType(foodType, function (data) {
        callback(data);
    });
}